package com.chushouya.manager.dao.repository;

import com.chushouya.manager.dao.entity.UserExtendEntity;
import com.chushouya.manager.dao.mapper.UserExtendEntityMapper;
import com.general.framework.data.mybatis.BaseRepository;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

@Repository
public class UserExtendRepository extends BaseRepository<UserExtendEntityMapper, UserExtendEntity, Long> {

    public UserExtendEntity selectByUserId(Long userId) {
        final Example example = this.buildExample();
        example.and().andEqualTo("userId", userId).andEqualTo("delFlag", 0);
        return this.selectFirstOneByExample(example);
    }

    public UserExtendEntity selectByUnionId(String unionId) {
        final Example example = this.buildExample();
        example.and().andEqualTo("unionId", unionId).andEqualTo("delFlag", 0);
        return this.selectFirstOneByExample(example);
    }

    /**
     * 根据appId和openId查询用户
     */
    public UserExtendEntity selectByAppIdAndOpenId(String appId, String openId) {
        final Example example = this.buildExample();
        example.and().andEqualTo("appId", appId).andEqualTo("openId", openId).andEqualTo("delFlag", 0);
        return this.selectFirstOneByExample(example);
    }

    /**
     * 根据openId查询用户
     */
    public UserExtendEntity findByOpenId(String openId) {
        final Example example = this.buildExample();
        example.and().andEqualTo("openId", openId).andEqualTo("delFlag", 0);
        return this.selectFirstOneByExample(example);
    }

    /**
     * 根据appId和userId查询用户
     */

    public UserExtendEntity selectByAppIdAndUserId(String appId, Long userId) {
        final Example example = this.buildExample();
        example.and().andEqualTo("appId", appId).andEqualTo("userId", userId).andEqualTo("delFlag", 0);
        return this.selectFirstOneByExample(example);
    }

    public List<UserExtendEntity> selectListByUserId(Long userId) {
        final Example example = this.buildExample();
        example.and().andEqualTo("userId", userId).andEqualTo("delFlag", 0);
        return this.selectByExample(example);
    }
}
